package org.yang.loginunion.core.password;

import org.apache.commons.lang3.StringUtils;
import org.yang.loginunion.core.LoginClient;
import org.yang.loginunion.core.constants.IdentityType;
import org.yang.loginunion.core.exception.LoginException;
import org.yang.loginunion.core.repository.OauthRepository;
import org.yang.loginunion.core.repository.PasswordRepository;

/**
 * Description:用户名+密码登录
 *
 * @author Billyang
 * @date 2024/8/4 16:53
 */
public class UserNamePasswordClient implements LoginClient {

    private OauthRepository oauthRepository;
    private PasswordRepository passwordRepository;

    public UserNamePasswordClient(OauthRepository oauthRepository, PasswordRepository passwordRepository) {
        this.oauthRepository = oauthRepository;
        this.passwordRepository = passwordRepository;
    }

    @Override
    public String login(String username, String password, String ignore3) {
        if (StringUtils.isAnyBlank(username, password)) {
            throw new LoginException("缺失参数");
        }

        String userId = oauthRepository.getUserId(IdentityType.USERNAME, username);
        if (userId == null) {
            throw new LoginException("用户名错误");
        }

        boolean flag = passwordRepository.checkPassword(userId, password);
        if (!flag) {
            throw new LoginException("密码不正确");
        }
        return userId;
    }
}
